export const formConfig = [
    {
        name: 'scope_id_list',
        type: 'cascader',
        placeholder: '筛选范围',
        options: [],
    },
    {
        name: 'device_name',
        type: 'input',
        placeholder: '设备名称',
    },
    {
        name: 'sensor_name',
        type: 'input',
        placeholder: '测点名称',
    },
    {
        name: 'modeltype',
        type: 'select_id',
        placeholder: '模型类别',
        options: [
            {
                id: '固定阈值',
                name: '固定阈值',
            },
            {
                id: '动态阈值',
                name: '动态阈值',
            },
            {
                id: '趋势预测',
                name: '趋势预测',
            },
            {
                id: '机理模型',
                name: '机理模型',
            },
            {
                id: 'AI模型',
                name: 'AI模型',
            },
        ],
    },
]
export const dialogConfig = [
    {
        name: 'scope_id_list',
        type: 'cascader',
        placeholder: '选择区域',
        required: true,
        options: [],
        onChange: () => {},
    },
    {
        name: 'trouble',
        type: 'cascader',
        placeholder: '选择模型',
        required: true,
        options: [],
        multiple: true,
        filterable: true,
    },
    {
        name: 'istest',
        type: 'switch',
        options: {
            active: { text: '线上', value: 2 },
            inactive: { text: '测试', value: 1 },
        },
        label: '轴数',
    },
]
export const editConfig = [
    {
        name: 'sensor_id',
        type: 'select_',
        placeholder: '选择测点',
        required: true,
        options: [],
    },
    {
        name: 'channel',
        type: 'select_',
        placeholder: '数据方向',
        required: true,
        options: [
            { label: 'X', value: 'X' },
            { label: 'Y', value: 'Y' },
            { label: 'Z', value: 'Z' },
            { label: 'A', value: 'A' },
            { label: 'B', value: 'B' },
            { label: 'C', value: 'C' },
        ],
    },
]
export const viewConfig = [
    {
        name: 'device_name',
        type: 'input',
        placeholder: '设备名称',
    },
    {
        name: 'sensor_name',
        type: 'input',
        placeholder: '测点名称',
    },
    {
        name: 'channel',
        type: 'input',
        placeholder: '数据方向',
    },
    {
        name: 'modeltype',
        type: 'input',
        placeholder: '模型类型',
    },
    {
        name: 'typecode',
        type: 'input',
        placeholder: '故障大类',
    },

    {
        name: 'trouble',
        type: 'input',
        placeholder: '模型大类名称',
    },
]
const status_dir = {
    1: '开启中',
    0: '已关闭',
}
const istest_dir = {
    1: '测试版本',
    2: '真实应用',
    null: '测试版本',
}
//表格
export const column = [
    {
        label: '设备名称',
        name: 'device_name',
    },
    {
        label: '测点名称',
        name: 'sensor_name',
    },
    {
        label: '故障大类名称',
        name: 'trouble',
    },
    {
        label: '模型类别',
        name: 'modeltype',
    },
    {
        label: '故障模型代码',
        name: 'warncode',
    },
    {
        label: '故障描述',
        name: 'description',
    },
    {
        label: '状态',
        name: 'status',
        format: (val) => status_dir[val],
        color: (val) => (val === 1 ? '#2D9E4C' : '#808080'),
    },
    {
        label: '说明',
        name: 'istest',
        format: (val) => istest_dir[val],
    },
    {
        title: '操作',
        name: 'action',
        width: '220',
        buttons: [],
    },
]
export const viewForm = {
    d_sigma: null,
    device_name: null,
    dynamic: null,
    dynamic_time: null,
    featureName: null,
    fixed: null,
    fixed_time: null,
    id: null,
    limit_num: null,
    maxThred: null,
    pred_time: null,
    sensor_id: null,
    sensor_name: null,
    trends: null,
    trends_time: null,
    warn_codes: null,
}
export const processData = (data) => {
    let processedData = {}
    for (let item of data) {
        if (!processedData[item.typecode]) {
            processedData[item.typecode] = {
                label: item.trouble,
                value: item.typecode,
                children: [],
            }
        }

        let child = {
            label: item.modelname + item.description,
            value: item.warncode,
        }
        if (item.modelcode) processedData[item.typecode].children.push(child)
    }

    return Object.values(processedData)
}
export const processEigArr = (arr, single, direction) => {
    let processedArr = []
    for (let item of arr) {
        if (single === 1 && item.vibration === 3) {
            let newItem = { ...item }
            if (direction === 'V') {
                newItem.name = '垂直' + newItem.name
                newItem.nickname = 'z_' + newItem.nickname
            } else if (direction === 'A') {
                newItem.name = '轴向' + newItem.name
                newItem.nickname = 'x_' + newItem.nickname
            } else if (direction === 'H') {
                newItem.name = '水平' + newItem.name
                newItem.nickname = 'y_' + newItem.nickname
            }
            processedArr.push(newItem)
        } else if (single === 2 && item.vibration === 3) {
            let newItem1 = { ...item }
            let newItem2 = { ...item }
            let newItem3 = { ...item }

            newItem1.name = '垂直' + newItem1.name
            newItem1.nickname = 'z_' + newItem1.nickname
            processedArr.push(newItem1)

            newItem2.name = '轴向' + newItem2.name
            newItem2.nickname = 'x_' + newItem2.nickname
            processedArr.push(newItem2)
            newItem3.name = '水平' + newItem3.name
            newItem3.nickname = 'y_' + newItem3.nickname
            processedArr.push(newItem3)
        } else {
            processedArr.push(item)
        }
    }
    return processedArr
}

export const formatData = (data) => {
    const result = []
    data.forEach((item) => {
        if (item.modeltype && item.trouble && item.typecode && item.warncode) {
            let modelType = result.find((item1) => item1.value === item.modeltype)
            if (!modelType) {
                modelType = {
                    label: item.modeltype,
                    value: item.modeltype,
                    children: [],
                }
                result.push(modelType)
            }
            let troubleType = modelType.children.find((item2) => item2.value === item.typecode)
            if (!troubleType) {
                troubleType = {
                    label: item.trouble,
                    value: item.typecode,
                    children: [],
                }
                modelType.children.push(troubleType)
            }
            const warnCode = {
                label: `${item.description} `,
                value: item,
            }
            troubleType.children.push(warnCode)
        }
    })
    return result
}

export const arrayToObject = (arr) => {
    return arr.reduce((acc, cur) => {
        try {
            acc[cur.config_index] = cur.config_value == '' || cur.config_value.includes('null') || cur.config_value.includes('None') ? null : cur.config_value
        } catch {
            acc[cur.config_index] = cur.config_value
        }

        return acc
    }, {})
}

export const mergeConfigs = (a1, a2) => {
    let a3 = []
    a1.forEach((item) => {
        if (a2.hasOwnProperty(item.config_index)) {
            const value = a2[item.config_index]
            const configValue = value !== null && value !== undefined ? value : item.config_value
            a3.push({ ...item, config_value: configValue })
        }
    })
    return a3
}
